package com.example.wechat.wechat;

import android.app.Activity;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.*;
import android.widget.*;
import java.util.List;

public class MassageAdapter extends RecyclerView.Adapter<MassageAdapter.ViewHolder>
{
    private List<Massage> mMassageList;

    public MassageAdapter( List<Massage> massageList) {
       mMassageList = massageList;
    }

    static class ViewHolder extends RecyclerView.ViewHolder{
        LinearLayout leftLayout;
        LinearLayout rightLayout;
        ImageView left_avatar;
        ImageView right_avatar;
        TextView left_msg;
        TextView right_msg;

        public ViewHolder(View view){
            super(view);
            leftLayout = (LinearLayout)view.findViewById(R.id.left_layout);
            rightLayout = (LinearLayout)view.findViewById(R.id.right_layout);
            left_avatar = (ImageView) view.findViewById(R.id.left_avatar);
            right_avatar = (ImageView) view.findViewById(R.id.right_avatar);
            left_msg = (TextView) view.findViewById(R.id.left_msg);
            right_msg = (TextView) view.findViewById(R.id.right_msg);
        }
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
        View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.massage_item, viewGroup ,false);
        ViewHolder holder = new ViewHolder(view);

        return holder;
    }

    @Override
    public void onBindViewHolder(ViewHolder viewHolder, int i) {
        Massage massage = mMassageList.get(i);
        if(massage.getType() == Massage.TYPE_RECEIVED)
        {
            //如果是收到的信息，则显示左边的消息布局，将右边的消息布局隐藏
            viewHolder.leftLayout.setVisibility(View.VISIBLE);
            viewHolder.rightLayout.setVisibility(View.GONE);
            viewHolder.left_avatar.setImageResource(massage.getImageId());
            viewHolder.left_msg.setText(massage.getContent());
        }
        else if (massage.getType() == Massage.TYPE_SENT)
        {
            //如果是发出的信息，则显示右边的消息布局，将左边的消息布局隐藏
            viewHolder.rightLayout.setVisibility(View.VISIBLE);
            viewHolder.leftLayout.setVisibility(View.GONE);
            viewHolder.right_avatar.setImageResource(massage.getImageId());
            viewHolder.right_msg.setText(massage.getContent());
        }

    }

    @Override
    public int getItemCount() {
        return mMassageList.size();
    }


}
